<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true"  @clickLeft="leftAction" :title="title" ></uni-nav-bar>	
		<view class="content">
			<form @submit="formSubmit">
				<view class="uni-form-item uni-row">
					<view class="title">基站ID</view>
					<input class="uni-input" name="baseStationId" placeholder="请输入基站ID" :value="form.baseStationId"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">基站名</view>
					<input class="uni-input" name="name" placeholder="请输入基站名" :value="form.name"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">位置</view>
					<input class="uni-input" name="address" placeholder="请输入位置" :value="form.address"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">备注</view>
					<input class="uni-input" name="remark" placeholder="请输入备注" :value="form.remark"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">端口</view>
					<input class="uni-input" name="port" placeholder="请输入端口" :value="form.port"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">请求点名</view>
					<input class="uni-input" name="sendCmdTagName" placeholder="请输入请求点名" :value="form.sendCmdTagName"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">接收点名</view>
					<input class="uni-input" name="reqTagName" placeholder="请输入接收点名" :value="form.reqTagName"/>
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">终端列表</view>
					<input class="uni-input" name="clientListTagName" placeholder="请输入终端列表点名" :value="form.clientListTagName"/>
				</view>
				<view class="uni-btn-v">
					<button form-type="submit">保存</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	
	export default {
		data() {		
			return{
				form:{}
			}
		},
		onLoad(options) {
			// console.log("isEdit :" + options.isEdit)
			if (options.isEdit == 1){
				this.title = "修改"
				this.bs_id = options.bs_id
				this.$http.get('/netty/station/'+this.bs_id).then(res=>{
					this.form = res.data.data
				}).catch( err =>{
					uni.showModal({
						content: '出错了，请稍后再试',
						showCancel: false
					})
				})
			}else{
				this.title = "新增"
			}
			// console.log("当前标题是:" + this.title)
			
		},
		components: {uniNavBar},
		methods: {
			leftAction(){
				uni.navigateBack({
					delta:1
				});
			},
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				if(this.title == "新增"){
					
					try {
					    const value = uni.getStorageSync('login_user');
					    if (value) {
							var time = this.$moment().format("yyyy-MM-DD HH:mm:ss");
							console.log("当前登录用户：" + value + "time:" + time)
							
							const params = {
								...e.detail.value,
								pId:1,
								createTime:time,
								updateTime:time,
								lastUpdateUser:value
							}
							console.log(params)
							this.$http.post('/netty/station/',params).then(res =>{
								console.log(res)
								if(res.data.code == 200){
									uni.showModal({
										content:'保存成功！',
										showCancel:false,
										success:(res)=>{
											uni.navigateBack({
												delta:1
											});
										}
									})
								}else{
									uni.showModal({
										content: '出错了，请稍后再试',
										showCancel: false
									})
								}
								
							}).catch(e =>{
							});
					    }else{
							uni.showModal({
								content: '出错了，请稍后再试',
								showCancel: false
							})
						}
					} catch (e) {
					    uni.showModal({
					    	content: '出错了，请稍后再试',
					    	showCancel: false
					    })
					}
				}else{
					
					try {
					    const value = uni.getStorageSync('login_user');
					    if (value) {
							var time = this.$moment().format("yyyy-MM-DD HH:mm:ss");
							console.log("当前登录用户：" + value + "time:" + time)
							
							const params = {
								...e.detail.value,
								pId:1,
								updateTime:time,
								lastUpdateUser:value,
								id:this.bs_id
							}
							console.log(params)
							this.$http.put('/netty/station/',params).then(res =>{
								console.log(res)
								if(res.data.code == 200){
									uni.showModal({
										content:'修改成功！',
										showCancel:false,
										success:(res)=>{
											uni.navigateBack({
												delta:1
											});
										}
									})
								}else{
									uni.showModal({
										content: '出错了，请稍后再试',
										showCancel: false
									})
								}
								
							}).catch(e =>{
							});
					    }else{
							uni.showModal({
								content: '出错了，请稍后再试',
								showCancel: false
							})
						}
					} catch (e) {
					    uni.showModal({
					    	content: '出错了，请稍后再试',
					    	showCancel: false
					    })
					}
				}
				
			}
		}
		
	}
</script>

<style lang="scss">
	@import '@/common/uni.css';
	.uni-form-item .title {
		margin-left: 20rpx;
		
		padding: 20rpx 0;
		width: 150rpx;
		font-size: 30rpx;
	}
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.uni-btn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		height: 40rpx;
	}
	
	@import '@/common/uni-nvue.scss';
	
</style>
